package leetcode_401_500;

import java.net.Inet4Address;
import java.util.*;

public class LeeCode_435 {
    public static void main(String[] args) {
//        System.out.println(eraseOverlapIntervals(new int[][]{{0, 2}, {1, 3}, {2, 4}, {3, 5},{4,6}}));
        System.out.println(eraseOverlapIntervals(new int[][]{{-52,31}, {-73,-26}, {82, 97}, {-65, -11},{-62,-49},
                {95,99},{58,95},{-31,49},{66,98},{-63,2},{30,47},{-40,-26}}));
    }
    private static int eraseOverlapIntervals(int[][] intervals) {
        int ans = 1;
        Set<Integer> used = new HashSet<>();
        Arrays.sort(intervals, Comparator.comparingInt(o -> o[1]));
        int n = intervals.length;
        int pre = intervals[0][1];
        for (int i = 1; i < n; i++) {
            if (intervals[i][0] >= pre){
                ans++;
                pre = intervals[i][1];
            }
        }
        return n - ans;
    }
}
